package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 分类接口业务层
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper ;
    @Autowired
    private DishMapper dishMapper ;
    @Autowired
    private SetmealMapper setmealMapper ;

    /**
     * 分类分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //设置分页查询起始参数
        PageHelper.startPage(categoryPageQueryDTO.getPage() , categoryPageQueryDTO.getPageSize()) ;

        //开始分页查询
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO) ;

        //封装返回结果
        Long total = page.getTotal() ;
        List<Category> categoryList = page.getResult() ;
        PageResult pageResult = new PageResult(total , categoryList) ;

        return pageResult ;
    }

    /**
     * 新增分类
     *
     * @param categoryDTO
     */
    @Override
    public void save(CategoryDTO categoryDTO) {
        //构造实体类对象
        Category category = new Category() ;

        //属性拷贝
        BeanUtils.copyProperties(categoryDTO , category);

        /*
        //获取当前登录用户的 id
        Long empId = BaseContext.getCurrentId() ;

        //设置创建人和时间等信息
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(empId);
        category.setUpdateUser(empId);
         */

        //初始状态设置为禁用
        category.setStatus(StatusConstant.DISABLE);

        //插入新数据
        categoryMapper.insert(category) ;
    }

    /**
     * 启用或禁用员工信息
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //获取当前登录用户的 id
        Long empId = BaseContext.getCurrentId() ;
        //构造一个实体类
        Category category = Category.builder()
                .id(id)
                .status(status)
                /*
                .updateTime(LocalDateTime.now())
                .updateUser(empId)
                 */
                .build() ;

        //只用传这几个参数进去

        //调用修改信息的接口
        categoryMapper.update(category) ;
    }

    /**
     * 修改分类
     *
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        //构造实体类对象
        Category category = new Category() ;
        BeanUtils.copyProperties(categoryDTO , category);

        /*
        //获取当前登录用户的 id
        Long empId = BaseContext.getCurrentId() ;
        //补全参数
        category.setUpdateUser(empId);
        category.setUpdateTime(LocalDateTime.now());
         */

        //修改
        categoryMapper.update(category);
    }

    /**
     * 根据ID删除分类
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //检查当前分类下是否有菜品 如果有 则不能删除
        Integer count = dishMapper.countByCategoryId(id) ;
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //检查当前分类下是否有套餐 如果有 则不能删除
        count = setmealMapper.countByCategoryId(id) ;
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL) ;
        }

        //删除分类数据
        categoryMapper.deleteById(id) ;
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        //没有分页查询 直接查询结果
        return categoryMapper.list(type) ;
    }
}
